class Admin::CompaniesController < AdminController

  def new
    @company = Company.new
  end

  def create
    @company = Company.new params[:company]
    success = @company && @company.save
    flash[:notice] = success && @company.errors.empty? ? t(:company_created) : t(:error_company_creation)
    redirect_to :back
  end
  
  def update
    object
    success = @company.update_attributes(params[:company])
    flash[:notice] = success && @company.errors.empty? ? t(:company_updated) : t(:error_company_update)
    redirect_to :back
  end

  def add_branch_office
    object
    @branch_office = BranchOffice.new(params[:branch_office].merge({:company_id => @company.id}))
    @branch_office.save!
    render :update do |page|
      page.insert_html :bottom, 'company_branch_offices', :partial => 'branch_office', :locals => { :bo => @branch_office }
      page.visual_effect :highlight, 'company_branch_offices', :duration => 0.5
    end
  end

  def update_branch_office
    object
    @branch_office = BranchOffice.find(params[:branch_office_id])
    @branch_office.update_attributes! params[:branch_office]
    render :update do |page|
      page.replace_html "branch_office_#{@branch_office.id}", :partial => 'branch_office', :locals => { :bo => @branch_office }
      page.visual_effect :highlight, 'company_branch_offices', :duration => 0.5
    end
  end

  def edit_branch_office
    object
    @branch_office = BranchOffice.find(params[:branch_office_id])
    render :partial => 'edit_branch_office', :locals => { :bo => @branch_office }
  end

  def remove_branch_office
    object
    @branch_office = BranchOffice.find(params[:branch_office_id])
    @branch_office.destroy
    render :text => t(:removed)
  end

  edit.before do
    @branch_offices = @company.branch_offices
  end

  destroy.wants.html { redirect_to :back }

  private

  def collection
    @companies ||= Company.all
  end

  def object
    @company ||= Company.find(params[:id])
  end

  def model_name
    'company'
  end

end
